package juc.imooccache;

import juc.imooccache.computable.MayFail;
import juc.threadlocal.ThreadLocalNormalUsafeResolve;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author fangbin
 * @desc
 * @date 2020/7/6 7:48
 */
public class CountDownTest {

    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        ImoocCache10<String,Integer> imoocCache10 = new ImoocCache10<>(new MayFail());
        long start = System.currentTimeMillis();
        for(int i=0;i<100;i++){
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    Integer result = null;
                    try {
                        System.out.println(Thread.currentThread().getName()+"准备就绪");
                        countDownLatch.await();
                        SimpleDateFormat dateFormat = ThreadSafeFormmater.dataFormatter.get();
                        String date = dateFormat.format(new Date());
                        System.out.println(date+Thread.currentThread().getName()+"开始执行");

                        result = imoocCache10.compute("666");
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("result:"+result);
                }
            });
        }
        Thread.sleep(5000);
        countDownLatch.countDown();
        executorService.shutdown();
    }
}

class ThreadSafeFormmater{

    public static ThreadLocal<SimpleDateFormat> dataFormatter = new ThreadLocal<SimpleDateFormat>(){
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("mm:ss");
        }
        @Override
        public SimpleDateFormat get(){
            return super.get();
        }
    };



}
